package com.price.util;


import android.util.Base64;


import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AndroidAesUtil {

    private static final String ALGORITHM = "AES";
    private static final String CIPHER_MODE = "CBC";
    private static final String PADDING_MODE = "PKCS7Padding";

    private static final String EN_CODE = "UTF-8";
    private static final int IV_LENGTH = 16;


    public static final String KEY;

    static {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH", Locale.ENGLISH);
        String date = dateFormat.format(new Date());
        KEY = "SolutionExpert" + date.substring(0, 4)
                + "se" + date.substring(4, 6)
                + "bio" + date.substring(6, 8)
                + "tec" + date.substring(8);
    }

    public static String encrypt(String key, String iv, String plainText) {
        try {
            byte[] keyBytes = generateKeyBytes(key);
            byte[] ivBytes = generateIvBytes(iv);
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, ALGORITHM);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

            Cipher cipher = Cipher.getInstance(ALGORITHM + "/" + CIPHER_MODE + "/" + PADDING_MODE);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

            byte[] cipherText = cipher.doFinal(plainText.getBytes(EN_CODE));

            return Base64.encodeToString(cipherText, Base64.NO_WRAP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return plainText;
    }

    public static String decrypt(String key, String iv, String cipherText) {
        try {
            byte[] keyBytes = generateKeyBytes(key);
            byte[] ivBytes = generateIvBytes(iv);

            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, ALGORITHM);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

            Cipher cipher = Cipher.getInstance(ALGORITHM + "/" + CIPHER_MODE + "/" + PADDING_MODE);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);

            byte[] plainTextBytes = cipher.doFinal(Base64.decode(cipherText, Base64.NO_WRAP));

            return new String(plainTextBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] generateKeyBytes(String key) throws UnsupportedEncodingException {
        // Perform necessary operations to generate key bytes
        // For example, using a hash function, a key derivation function, or a random number generator
        // Make sure the generated key bytes are 32 bytes long for DES
        // Here, I simply assume the key is already in the correct format and length

        return key.getBytes(EN_CODE);
    }

    private static byte[] generateIvBytes(String iv) throws UnsupportedEncodingException {
        // Perform necessary operations to generate IV bytes
        // For example, using a hash function, a key derivation function, or a random number generator
        // Make sure the generated IV bytes are 16 bytes long for CBC mode
        return Base64.decode(iv, Base64.NO_WRAP);
    }
}
